import { getLists, getClassifyList, getSaves, removesFn } from '@/api/msbd.js'

export default {
    namespaced: true,
    state: {
        list: [],
        classifyList: [],
        pages: {
            pages: 0,
            pagesize: 5,
            keyword: '',
            by: 'desc',
            order: 'id'
        },
        total: 0
    },
    mutations: {
        setList(state, obj) {
            state.list = obj.data.resultes
            // console.log(obj.total);
            state.total = obj.total
        },
        setClassifyList(state, arr) {
            state.classifyList = arr.data.resultes
        },
        setpages(state, page) {
            state.pages.pages = page
        },
        setKeyword(state, val) {
            state.pages.keyword = val
            state.pages.pages = 0
        },
        setSort(state, clo) {
            console.log(clo);
            if (clo.order === 'ascending') {
                state.pages.by = 'asc'
                state.pages.order = 'date'
            } else if (clo.order === 'descending') {
                state.pages.by = 'desc'
                state.pages.order = 'date'
            } else {
                state.pages.by = 'desc'
                state.pages.order = 'id'
            }
        },
    },
    actions: {
        async getList({ commit, dispatch, state }) {
            const res = await getLists(state.pages)
            console.log(res);
            commit('setList', res.data)
        },
        async getClassify({ commit, dispatch }) {
            const row = await getClassifyList()
            console.log(row);
            commit('setClassifyList', row.data)
        },
        async getSave({ commit, dispatch }, form) {
            const res = await getSaves(form)
            // console.log(res);
            dispatch('getList')
        },
        async getDelete({ commit, dispatch }, ids) {
            await removesFn(ids)
            dispatch('getList')
        },
        async getPages({ commit, dispatch }, page) {
            console.log(page);
            commit('setpages', page)
            dispatch('getList')
        },
        async getKeyword({ commit, dispatch }, val) {
            commit('setKeyword', val)
            dispatch('getList')
        },
        //排序
        async getSort(context, val) {
            context.commit('setSort', val)
            context.dispatch('getList')
        }
    },
    getters: {}
}